#include "spi.h"


void SPI1_Init(void)
{
	SPI_InitStructure SPI_initStruct;

	GPIO_Init(SPI1_CS_PORT, SPI1_CS_PIN,  1, 0, 0);				                        //CS

	GPIO_SetBit(SPI1_CS_PORT, SPI1_CS_PIN);

	PORT_Init(SPI1_SCLK_PORT, SPI1_SCLK_PIN, FUNMUX1_SPI1_SCLK, 0);
	PORT_Init(SPI1_MOSI_PORT, SPI1_MOSI_PIN, FUNMUX0_SPI1_MOSI, 0);
//	PORT_Init(_SPI0MISOPORT_, _SPI0MISOPIN_, FUNMUX1_SPI1_MISO, 1);
	
	SPI_initStruct.clkDiv = SPI_CLKDIV_4;
	SPI_initStruct.FrameFormat = SPI_FORMAT_SPI;
	SPI_initStruct.SampleEdge = SPI_FIRST_EDGE;
	SPI_initStruct.IdleLevel = SPI_LOW_LEVEL;
	SPI_initStruct.WordSize = 8;
	SPI_initStruct.Master = 1;
	SPI_initStruct.RXHFullIEn = 0;
	SPI_initStruct.TXEmptyIEn = 0;
	SPI_initStruct.TXCompleteIEn = 0;
	SPI_Init(SPI1, &SPI_initStruct);
	SPI_Open(SPI1);
}


void SPI1_ReadWrite(uint8_t data,uint8_t mode)
{
	uint32_t  dat;
	dat = data;
	if(mode == 1){
		dat |= 0x100;
		
	}
	GPIO_ClrBit(SPI1_CS_PORT, SPI1_CS_PIN);
	SPI_WriteWithWait(SPI1, dat);
	GPIO_SetBit(SPI1_CS_PORT, SPI1_CS_PIN);
}
















